﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMWJamecl.BusinessEntities;
using SMWJamecl.Repository;

namespace SMWJamecl.Repository
{
   public class VehiculoRepository:MasterRepository, IVehiculoRepository
    {

        public List<Vehiculo> GetVehiculosByCriterio(string criterio)
        {
            var query = from c in Context.Vehiculoes.Include("Marca").Include("Modelo")
                        select c;
            if (criterio != null && criterio != "" && criterio != string.Empty)
            {
                query = from c in query
                        where c.Placa.ToUpper().Contains(criterio.ToUpper()) ||
                              c.Color.ToUpper().Contains(criterio.ToUpper())
                        select c;
            }

            return query.ToList();
        }

        public Vehiculo GetVehiculoByPlaca(string placa)
        {
            var query = from c in Context.Vehiculoes
                        where c.Placa.Equals(placa)
                        select c;
            return query.SingleOrDefault();
        }

        public void AddVehiculo(Vehiculo vehiculo)
        {
            Context.Vehiculoes.Add(vehiculo);
            Context.SaveChanges();
        }

        public void UpdateVehiculo(Vehiculo vehiculo)
        {
            Context.Entry(vehiculo).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveVehiculo(string placa)
        {
            var existe = Context.Vehiculoes.Find(placa);

            if (existe != null)
            {
                Context.Vehiculoes.Remove(existe);
                Context.SaveChanges();
            }
        }
       
        public List<Vehiculo> GetAllFromVehiculos()
        {
            var query = from c in Context.Vehiculoes
                        select c;
            return query.ToList();
        }
    }
}
